<template>
  <div style="padding-top: 16px;">
    <z-input v-model="inputVal"></z-input>
    <z-button @click="createLeaveMessageMethod">留言</z-button>
    <div class="showList">
      <div class="showItem" v-for="item in showList">
        <div class="showName">{{item.id}}</div>
        <div class="showContent">{{item.content}}</div>
        <div class="leaveTime">{{formateTime(item.createdAt)}}</div>
      </div>
    </div>
  </div>
</template>
<script>
  import {zInput, zButton} from '../../../src/main'
  import {createLeaveMessage, findAllMessage} from '../../service'
  import moment from 'moment'
  export default {
    components: {
      zInput, zButton
    },
    data () {
      return {
        showList: [],
        inputVal: ''
      }
    },
    mounted() {
      this.findAllMessageMethod()
    },
    methods: {
      async createLeaveMessageMethod() {
        await createLeaveMessage(this.inputVal || '默认留言')
        this.findAllMessageMethod()
      },
      findAllMessageMethod() {
        findAllMessage().then(res => {
          console.log('data', res.data)
          this.showList = res.data
        })
      },
      formateTime(time) {
        if (time) {
          return moment(Date.now(time)).format('YYYY-MM-DD')
        }
      }
    }
  }
</script>

<style scoped>
.showList {
  display: flex;
  margin-top: 20px;
  flex-direction: column;
}
.showItem {
  min-height: 20px;
  display: flex;
  justify-content: space-between;
}
.showName {
  margin-right: 20px;
}
.showContent {
  flex: 1;
}
</style>